package com.mrx.bot.telegram.config;

import com.mrx.bot.telegram.model.BotConfig;
import com.mrx.bot.telegram.utils.BotConfigLoader;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import okhttp3.OkHttpClient;
import org.telegram.telegrambots.longpolling.util.TelegramOkHttpClientFactory;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
 * @author Mr.X
 * @since 2025/6/13 21:02
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OkhttpConfig extends TelegramOkHttpClientFactory.DefaultOkHttpClientCreator {

    public static final OkhttpConfig INSTANCE = new OkhttpConfig();

    @Override
    public OkHttpClient get() {
        BotConfig botConfig = BotConfigLoader.getBotConfig();
        Proxy.Type proxyType = botConfig.getProxyType();
        String host = botConfig.getProxyHost();
        Integer port = botConfig.getProxyPort();
        if (proxyType == null) {
            return super.get();
        }
        return getBaseClient()
                .proxy(new Proxy(proxyType, new InetSocketAddress(host, port)))
                .build();
    }

}
